import 'package:flutter/cupertino.dart';
import 'package:flutter_base_plugin/base_library/base/base_function.dart';
import 'package:flutter_base_plugin_example/config/sp_key.dart';
import 'package:flutter_base_plugin_example/models/img_code_model.dart';
import 'package:flutter_base_plugin_example/models/login_model.dart';
import 'package:flutter_base_plugin_example/net/http_urls.dart';

class LoginViewModel extends BaseViewModel {
  String username = '';
  String password = '';
  String code = "";
  ImgCodeModel? data;

  @override
  String getTag() {
    // TODO: implement getTag
    return "LoginViewModel";
  }

  Future getImgCode() async {
    await api.request<ImgCodeModel>(
      HttpUrls.captcha,
      Method.GET,
      onSuccess: (ImgCodeModel? bean) {
        data = bean;
        notifyListeners();
        // SpUtils.putString(SpKey.spAppToken, bean!.token);
      },
    );
  }

  Future onLogin(BuildContext context) async {
    var params = {
      "account": username,
      "password": password,
      // "captcha": code,
      // "captcha_id": data?.captchaId,
    };
    await api.request<LoginModel>(
      HttpUrls.login,
      Method.POST,
      params: params,
      isShowLoading: true,
      loadingText: "登录中···",
      onSuccess: (LoginModel? bean) {
        SpUtils.putString(SpKey.spAppToken, bean!.token);
        Navigator.pop(context);
        ToastUtils.show("登录成功");
      },
    );
  }
}
